package com.example.minefieldkeepercloud.activity.machinerymaintain.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.bigkoo.pickerview.TimePickerView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.activity.dailypower.DailyPowerAc;
import com.example.minefieldkeepercloud.adapter.ListFmAdapter;
import com.example.minefieldkeepercloud.bean.FactoryBean;
import com.example.minefieldkeepercloud.bean.ListFmBean;
import com.example.minefieldkeepercloud.interfac.ApiHttpCallback;
import com.example.minefieldkeepercloud.network.MyGson;
import com.example.minefieldkeepercloud.network.NetHelperNew;
import com.example.minefieldkeepercloud.util.NetWorkUtil;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyFragmentPagerAdapter;
import com.example.minefieldkeepercloud.view.pop.FactorySelectPop;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ListFragment extends Fragment implements LazyFragmentPagerAdapter.Laziable {
    @BindView(R.id.ly_top)
    LinearLayout relative;
    @BindView(R.id.tv_FactoryName)
    TextView tv_FactoryName;
    @BindView(R.id.tv_FullName)
    TextView tv_FullName;

    private FactorySelectPop factorySelectPop;
    private TimePickerView pvTimeDay;//年月日时间选择器
    private String NowData="";
    private SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM");
    private String FactoryId="";
    private List<ListFmBean.DataBean> listData;
    private ListFmAdapter listFmAdapter;
    @BindView(R.id.recyleview)
    ListView recyleview;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_list, container, false);
        ButterKnife.bind(this, view);
        initview();
        getMineField();
        return view;
    }

    /**
     * 初始化
     */
    private void initview() {
        listData=new ArrayList<>();
        listFmAdapter=new ListFmAdapter((AppCompatActivity) getActivity(),listData);
        recyleview.setAdapter(listFmAdapter);
        recyleview.setDivider(null);
        //时间选择器
        pvTimeDay = new TimePickerView(getContext(), TimePickerView.Type.YEAR_MONTH);
        //设置范围
        pvTimeDay.setRange(2017, 2030);

        //设置是否循环滚动
        pvTimeDay.setCyclic(false);
        pvTimeDay.setCancelable(true);

        //时间选择后回调
        pvTimeDay.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                NowData=ymd.format(date);
                tv_FullName.setText(NowData);
                listFmAdapter.setDate(NowData);

            }


        });
        NowData = ymd.format(new Date(System.currentTimeMillis()));
        tv_FullName.setText(NowData);
        listFmAdapter.setDate(NowData);


    }

    private void getMineField() {
        if (!NetWorkUtil.check(getContext())) {
            return;
        }
        NetHelperNew.GetFactory(new ApiHttpCallback(getContext(), 1,true) {
            @Override
            public void onSuccess(String data) {
                FactoryBean factoryBean = MyGson.getInstance().fromJson(data, FactoryBean.class);
                if (factoryBean.getType() == 1) {
                    if (factoryBean.getData().size() > 0) {
                        CreatePop(factoryBean);
                    } else {
                        ToastUtil.showToast(getContext(), "没有可查询的矿场");
                    }

                } else {
                    ToastUtil.showToast(getContext(), factoryBean.getContent());
                }
            }
        });


    }
    private void CreatePop(FactoryBean dataBean) {
        if(FactoryId.isEmpty())
        {
            FactoryId=dataBean.getData().get(0).getID()+ "";
        }
        if (factorySelectPop == null) {
            factorySelectPop = new FactorySelectPop(getContext(), dataBean, new FactorySelectPop.OnSelectListener() {
                @Override
                public void select(int id, String name) {
                    FactoryId=id+"";
                    tv_FactoryName.setText(name);

                }
            });
        }

        tv_FactoryName.setText(dataBean.getData().get(0).getFactoryName());
        getNetData();
    }



    @OnClick({R.id.ly_user,R.id.ly_Minefield,R.id.search})
    public void onClick(View view) {
        switch (view.getId()) {


            case R.id.ly_user:
                pvTimeDay.setTime(new Date(System.currentTimeMillis()));
                pvTimeDay.show();
                break;
            case R.id.ly_Minefield:
                if (factorySelectPop != null)
                    factorySelectPop.showAsDropDown(relative, 0, 0);
                else
                    ToastUtil.showToast(getContext(), "没有可查询的矿场");
                break;
            case R.id.search:
                if (!NetWorkUtil.check(getContext())) {
                    return;
                }


                getNetData();
                break;
        }
    }

    private void getNetData()
    {

        NetHelperNew.GetErrInfoStatistics_Table(FactoryId, NowData, new ApiHttpCallback(getContext(), 1, true) {
            @Override
            public void onSuccess(String data) {
                ListFmBean listFmBean = MyGson.getInstance().fromJson(data, ListFmBean.class);
                if (listFmBean.getType() == 1) {
                    listData = listFmBean.getData();
                    listFmAdapter.setListData(listData);
                    listFmAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.showToast(getContext(), listFmBean.getContent());
                }
            }
        });

    }

}
